package com.lgh.hotel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lgh.hotel.dao.mapper.PunchMapper;
import com.lgh.hotel.dao.pojo.Guest;
import com.lgh.hotel.dao.pojo.Punch;
import com.lgh.hotel.dao.pojo.Table;
import com.lgh.hotel.service.GuestService;
import com.lgh.hotel.service.PunchService;
import com.lgh.hotel.service.TableServer;
import com.lgh.hotel.vo.PunchVo;
import com.lgh.hotel.vo.PunchVos;
import com.lgh.hotel.vo.Result;
import com.lgh.hotel.vo.params.PunchParams;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.tags.EditorAwareTag;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class PunchServiceImpl implements PunchService {

    @Autowired
    PunchMapper punchMapper;

    @Autowired
    GuestService guestService;

    @Autowired
    TableServer tableServer;


    @Override
    public Result findPunchAll() {
        return Result.succ(punchMapper.selectList(null));
    }

    @Override
    public Result addPunch(PunchParams params) {

        Punch punch = new Punch();
        BeanUtils.copyProperties(params,punch);
        punch.setPunchTime(System.currentTimeMillis());
        punch.setState(0);
        return Result.succ(punchMapper.insert(punch));

    }

    @Override
    public Result findPunchAllById(Long idCrad) {

        // 1.先把所有打卡数据获取 2.把long时间 转换成人看的时间 3.转换vo

        LambdaQueryWrapper<Punch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Punch::getIdCard,idCrad);
        List<Punch> punches = punchMapper.selectList(queryWrapper);
        List<PunchVo> PunchVos = copyList(punches,false,false);
        return Result.succ(PunchVos);
    }

    @Override
    public Result findall() {
        List<Guest> cards = guestService.findallIdCrad();
        //获取一个用户的所有打卡情况
        List<PunchVos> punchVos = toList(cards);
        return Result.succ(punchVos);
    }

    @Override
    public void deletepunch(Long idCard) {
        LambdaQueryWrapper<Punch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Punch::getIdCard,idCard);
        punchMapper.delete(queryWrapper);
    }

    @Override
    public Result audit() {
        LambdaQueryWrapper<Punch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Punch::getState,0);
        queryWrapper.orderByAsc(Punch::getPunchTime);
        List<Punch> punches = punchMapper.selectList(queryWrapper);

        List<PunchVo> punchVos = copyList(punches,true,true);

        return Result.succ(punchVos);
    }

    @Override
    public Result updateAudit(Punch punch) {
        return Result.succ(punchMapper.updateById(punch));
    }

    @Override
    public Result findpassid(Long id) {
        LambdaQueryWrapper<Punch> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Punch::getIdCard,id).orderByDesc(Punch::getPunchTime).last("limit 1");
        Punch punches = punchMapper.selectOne(queryWrapper);

        System.out.println(punches);
        if (punches.getState().equals(1)){
            return Result.succ(1);
        }

        return Result.fail(null);

    }

    private List<PunchVos> toList(List<Guest> cards) {

        List<PunchVos> punchVos = new ArrayList<>();

        for (Guest card : cards) {
            Long idCard = card.getIdCard();
            LambdaQueryWrapper<Punch> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Punch::getIdCard,idCard).eq(Punch::getState,1);
            List<Punch> punches = punchMapper.selectList(queryWrapper);
            //一个用户的所有打卡情况（只记录一天以内的一次）
            PunchVos punchVos1 = totime(punches,card.getName(),card.getIdCard());

            Table table = tableServer.findTableByIdCard(card.getIdCard());
            if (null != table) {
                punchVos1.setRoom(table.getRoom());
            }

Date now = new Date();
            long nowv = now.getTime();
            int betweenDays = (int)((nowv - card.getStartingTime()) / (1000 * 60 * 60 *24) );
            punchVos1.setNum(Integer.valueOf( betweenDays));
            punchVos.add(punchVos1);

        }

        return punchVos;

    }

    private PunchVos totime(List<Punch> punches,String name,Long idcard) {

        PunchVos punchVos = new PunchVos();
        punchVos.setNum(punches.size());
        punchVos.setName(name);
        punchVos.setIdCard(idcard);
        return punchVos;

    }

    private List<PunchVo> copyList(List<Punch> punches,boolean isName,boolean isRoom) {
        List<PunchVo> punchVos = new ArrayList<>();
        for (Punch punch : punches) {
            punchVos.add(copy(punch,true,true));
        }
        return punchVos;
    }

    private PunchVo copy(Punch punch,boolean isName,boolean isRoom) {
        PunchVo punchVo = new PunchVo();
        BeanUtils.copyProperties(punch,punchVo);
        Date date = new Date(punch.getPunchTime());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        punchVo.setTime(simpleDateFormat.format(date));

        if (isName){
            Long idCard = punch.getIdCard();
            Guest guestByIdCard = guestService.findGuestByIdCard(idCard);
            punchVo.setName(guestByIdCard.getName());
        }
        if (isRoom){
            Long idCard = punch.getIdCard();
            Table tableByIdCard = tableServer.findTableByIdCard(idCard);
            punchVo.setRoom(tableByIdCard.getRoom());
        }

        return punchVo;
    }


}
